package pl.planuz;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class CheckActivity2 extends ListActivity  implements OnClickListener{     
	TextView selection;
	public int idToModify;
	CheckActivity ca=new CheckActivity();
	database dm;
	Helper help;
	String nwydzial, nkierunek;
	boolean uruchom=false;
	Parser p=new Parser();
	List<String> list2 = null;
	

	List<String[]> list = new ArrayList<String[]>();
	List<String[]> names2 =null ;
	String[] stg1,stga1,stgab1;
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.check2);
		View button1Click = findViewById(R.id.button1);
		button1Click.setOnClickListener(this);
		if(uruchom==false){
			help=new Helper();
			uruchom=true;
			}
		  dm = new database(this,Helper.getwydzial(),null,null);
	      names2 = dm.selectK();
		stg1=new String[names2.size()]; 
		stga1=new String[names2.size()];
		stgab1=new String[names2.size()];

		int x=0,y=0,z=0;;
		String stg,stg2,stg3;

		for (String[] name : names2) {
			stg = name[2];
			stg1[x]=stg;
			x++;
		}

		for (String[] namea : names2) {
			stg2 = namea[0];
			stga1[y]=stg2;
			y++;
		}
		for (String[] nameab : names2) {
			stg3 = nameab[3];
			stgab1[z]=stg3;
			z++;
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(   
				this,android.R.layout.simple_list_item_1,   
				stg1);
        this.setListAdapter(adapter);
		selection=(TextView)findViewById(R.id.selection);
		
	}      

	public void onListItemClick(ListView parent, View v, int position, long id) {
		nkierunek=stg1[position];
		dm.setkierunek(stg1[position]);
		help.setkierunek(stg1[position]);
		try {
			list2 = p.pobierzKierunek(stgab1[position]);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(int i=0;i<list2.size();i=i+3){
		dm.insert2(Integer.parseInt(stga1[position]), list2.get(i),list2.get(i+2));
		}
		selection.setText(stg1[position]);
	}
	public void onClick(View v) {
		switch(v.getId()){

		case R.id.button1:
			Intent i = new Intent(this, CheckActivity3.class);
			startActivity(i);
			break;
		
	}
	}
	
}
